<!--
 * @Author: your name
 * @Date: 2021-12-22 18:25:29
 * @LastEditTime: 2021-12-23 00:34:11
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \briup_am\am-ui\src\pages\play\race\details.vue
-->
<template>
<div class="race_details">
  <el-button type="text" size="small" @click="backhandler">返回</el-button>
 <div class="race">
      <h3>比赛信息</h3>
<el-row>
        <el-col :span="12">
          <span class="label">赛事名称</span>
          <span class="value">{{race.name}}</span>
        </el-col>
        <el-col :span="12">
          <span class="label">赛事地点</span>
          <span class="value">{{race.serial_number}}</span>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <span class="label">比赛时间</span>
          <span class="value">{{race.status}}</span>
        </el-col>
        <el-col :span="12">
          <span class="label">报价</span>
          <span class="value">{{race.type}}</span>
        </el-col>
      </el-row>
     </div>
      <div>
      <h3>已接受球队</h3>
      <el-button size="small" type="primary" @click="toBindHandler">接受</el-button>
      <el-table size="small" :data="bindedDevices">
        <el-table-column label="球队编号" prop="serial_number"></el-table-column>
        <el-table-column label="球队名称" prop="name"></el-table-column>
        <el-table-column label="球队状态" prop="online_status">
          <template slot-scope="scope">
            <el-tag size="mini" type="success" v-if="scope.row.online_status == 1">备赛</el-tag>
            <el-tag size="mini" type="danger" v-else>休整</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="toOnlineHandler(scope.row)" v-if="scope.row.online_status == 0" >备赛</el-button>
            <el-button type="text" size="small" @click="toOfflineHandler(scope.row)" v-else>休整</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框 -->
    <el-dialog title="球队接收" :visible.sync="visible">
      <el-form :model="form" label-width="100px">
        <el-form-item label="球队" >
          <el-select v-model="form.device_id">
            <el-option 
              v-for="c in devices" 
              :key="c.id" 
              :label="c.name" 
              :value="c.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="submitHandler">确 定</el-button>
      </div>
    </el-dialog>  
  </div>
</template>
<script>
import {get} from '@/utils/request'
export default {
  data(){
    return {
      race:{},
      visible:false,
      form:{},
      devices:[],
      bindedDevices:[]
    }
  },
  mounted() {
    this.race = this.$route.query;
    // 查询所有未绑定的设备
    this.loadUnbindDevices();
    // 查询该工程上绑定的所有设备
    this.loadRaceDevices();
  },
  methods:{
    toOnlineHandler(row){
      let url = "/device/openDevice"
      get(url,{id:row.id}).then(resp =>{
        this.$message({type:"success",message:resp.message});
        this.loadRaceDevices();
      })
    },
    toOfflineHandler(row){
      let url = "/device/closeDevice"
       get(url,{id:row.id}).then(resp =>{
        this.$message({type:"success",message:resp.message});
        this.loadRaceDevices();
    })
    },
    // 加载该工程绑定的设备信息
    loadRaceDevices(){
      let url = "/device/pageQuery"
      let params = {page:1,pageSize:1000,race_id:this.race.id}
      get(url,params).then(resp => {
        this.bindedDevices = resp.data.list;
      })
    },
    loadUnbindDevices(){
      let url = "/device/pageQuery";
      let params = {page:1,pageSize:1000,bind_status:0}
      get(url,params).then(resp => {
        this.devices = resp.data.list;
      })
    },
    toBindHandler(){
      this.visible = true;
      this.form = {
        race_id:this.race.id
      }
    },
    submitHandler(){
      let url = "/race/bindService"
      get(url,this.form).then(resp => {
        this.$message({type:"success",message:resp.message});
        this.visible = false;
        this.loadRaceDevices();
        this.loadUnbindDevices();
      })
    },
    backhandler(){
      this.$router.go(-1);
    }
  }
}
</script>
\ No newline at end of file
</script>
<style scoped>
.race_details .race .label{
  font-weight: bold;
  padding-right: 5px;
  line-height: 2.4em;
}
</style>